"""
编写人 :  何小船
组名 :  第六组测试小可爱
"""
# 引包
from time import sleep
from Common.Base import Base, open_browser
from Page.LoginPage import LoginPage, Login_url

adderss_url = 'http://172.16.7.121:8080/ecshop/user.php'


# 创建测试类
class Shoppingaddress(Base):
    '''封装页面表现层 制作页面元素定位'''
    name_loc = ("name", "username")  # 用户名输入框
    password_loc = ("name", "password")  # 密码输入框
    Submit_loc = ('class name', 'us_Submit')
    user_center_loc = ('link text', '用户中心')
    shippingaddress_loc = ('xpath', '//div[@class="userMenu"]/a[4]')  # 用户中心界面收货地址定位

    def address_locator(self, index='last()'):
        provinces_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[2]')  # 省
        cities_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[3]')  # 市
        districts_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[4]')  # 区
        consignee_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[2]/td[2]/input')  # 收货人姓名
        email_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[2]/td[4]/input')  # 电子邮箱地址
        detailed_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[3]/td[2]/input')  # 详细地址
        zip_code_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[3]/td[4]/input')  # 邮政编码
        phone_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[4]/td[2]/input')  # 电话
        Mobile_phone_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[4]/td[4]/input')  # 手机
        updata_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input')  # 修改
        delete_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input[2]')  # 删除
        add_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input')  # 新增收货地址

        if index == 'last()':
            return {
                'provinces_loc': provinces_loc,
                'cities_loc': cities_loc,
                'districts_loc': districts_loc,
                'consignee_loc': consignee_loc,
                'email_loc': email_loc,
                'detailed_address_loc': detailed_address_loc,
                'zip_code_loc': zip_code_loc,
                'phone_loc': phone_loc,
                'Mobile_phone_loc': Mobile_phone_loc,
                'add_address_loc': add_address_loc
            }

        else:
            return {
                'provinces_loc': provinces_loc,
                'cities_loc': cities_loc,
                'districts_loc': districts_loc,
                'consignee_loc': consignee_loc,
                'email_loc': email_loc,
                'detailed_address_loc': detailed_address_loc,
                'zip_code_loc': zip_code_loc,
                'phone_loc': phone_loc,
                'Mobile_phone_loc': Mobile_phone_loc,
                'updata_loc': updata_loc,
                'delete_loc': delete_loc
            }

    def input_login_name(self, name):
        '''
        输入登录用户姓名
        :return:
        '''
        self.send_keys(name, self.name_loc)

    def input_login_password(self, password):
        '''
        输入登录用户密码
        :return:
        '''
        self.send_keys(password, self.password_loc)

    def click_Submit(self):
        '''
        点击登入
        :return:
        '''
        self.click(self.Submit_loc)

    def click_shoppingaddress(self):
        '''
        点击收货地址
        :return:
        '''
        self.click(self.shippingaddress_loc)

    def input_consignee(self, name, index='last()'):
        '''
        输入收货人姓名
        :param name: 收货人姓名
        :param index: 索引
        :return:
        '''
        self.send_keys(name, self.address_locator(index)['consignee_loc'])

    def input_email(self, email, index='last()'):
        '''
        输入电子邮箱地址
        :param email: 邮箱
        :param index: 索引
        :return:
        '''
        self.send_keys(email, self.address_locator(index)['email_loc'])

    def input_address(self, text, index='last()'):
        '''
        输入地址
        :param text: 地址
        :param index: 索引
        :return:
        '''
        self.send_keys(text, self.address_locator(index)['detailed_address_loc'])

    def input_zip_code(self, text, index='last()'):
        '''
        输入邮政编码
        :param text: 邮政编码
        :param index: 索引
        :return:
        '''
        self.send_keys(text, self.address_locator(index)['zip_code_loc'])

    def input_phone(self, number, index='last()'):
        '''
        输入电话号码
        :param number: 电话
        :param index: 索引
        :return:
        '''
        self.send_keys(number, self.address_locator(index)['phone_loc'])

    def input_Mobile_phone(self, number, index='last()'):
        '''
        输入手机号码
        :param number:
        :param index:
        :return:
        '''
        self.send_keys(number, self.address_locator(index)['Mobile_phone_loc'])

    def click_add_address(self, index='last()'):
        '''新增收货地址'''
        self.click(self.address_locator(index)['add_address_loc'])

    def click_updata(self, index):
        '''修改信息'''
        self.click(self.address_locator(index)['updata_loc'])

    def click_delete(self, index):
        '''删除地址'''
        self.click(self.address_locator(index)['delete_loc'])

    def login_adderss(self):
        '''

        :return:
        '''


if __name__ == '__main__':
    driver = open_browser()
    login = LoginPage(driver)
    login.open_url(Login_url)
    login.member_login('小乔', '123456')
    sleep(3)

    adder = Shoppingaddress(driver)
    adder.click_shoppingaddress()  # 点击收货地址

    # # 选择 省 市 区
    # adder.select_drop_down_menu(23, adder.address_locator()['provinces_loc'])
    # sleep(2)
    # adder.select_drop_down_menu(1, adder.address_locator()['cities_loc'])
    # sleep(2)
    # adder.select_drop_down_menu(11, adder.address_locator()['districts_loc'])

    # # 输入信息
    # adder.input_consignee('小何', 1)
    # adder.input_email('2399110597@qq.com', 1)
    # adder.input_address('成都', 1)
    # adder.input_zip_code('0554', 1)
    # adder.input_phone('17965485201', 1)
    # adder.input_Mobile_phone('17965485201', 1)

    # 修改信息
    # adder.click_updata(1)

    # 点击新增
    # adder.click_add_address()

    adder.click_delete(1)
    adder.get_alert_accept_text()

    sleep(2)
    adder.close_browser()
